﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Goldlight.Mvvm.Base.Infrastructure;
using System.Threading;
using System.Windows.Input;
using Goldlight.Mvvm.Base.Command;
using System.ComponentModel;
using Goldlight.Mvvm.Base;

namespace Goldlight.SampleApplication.ViewModels
{
    public class TestViewModel : EditableViewModelBase, IDataErrorInfo
    {
        #region Members
        private LanguageResources.Language _language;
        private ICommand _startEditing;
        private ICommand _commitEdit;
        private ICommand _rollbackEdit;
        private string _userName;
        private int _age;
        private bool _inEdit; 
        #endregion
        /// <summary>
        /// Initializes a new instance of <see cref="TestViewModel"/>
        /// </summary>
        public TestViewModel()
            : base()
        {
            _language = new LanguageResources.Language();
        }

        protected override void DesignData()
        {
            UserName = "Billy Bob Dishcloth";
            Age = 21;
            base.DesignData();
        }

        protected override void OnInitialize()
        {
            Validation<TestViewModel>.RegisterResource(this, typeof(LanguageResources.Language).FullName);
            Validation<TestViewModel>.AddValidation(this, "UserName", v => !string.IsNullOrEmpty(v.UserName), "NullUserName");
            base.OnInitialize();
        }

        /// <summary>
        /// Resources compiled up using extension from <see href="http://www.codeproject.com/KB/dotnet/ResXFileCodeGeneratorEx.aspx"/>
        /// </summary>
        public override object Resources
        {
            get
            {
                return _language;
            }
        }

        /// <summary>
        /// Get or set the user name.
        /// </summary>
        public string UserName
        {
            get
            {
                return _userName;
            }
            set
            {
                if (_userName != value)
                {
                    _userName = value;
                    OnChanged("UserName");
                }
            }
        }

        /// <summary>
        /// Get or set the age of the user.
        /// </summary>
        public int Age
        {
            get
            {
                return _age;
            }
            set
            {
                if (_age != value)
                {
                    _age = value;
                    OnChanged("Age");
                }
            }
        }

        public ICommand StartEdit
        {
            get
            {
                if (_startEditing == null)
                {
                    _startEditing = new RelayCommand(() => { this.BeginEdit(); _inEdit = true; }, ()=>!_inEdit);
                }
                return _startEditing;
            }
        }

        public ICommand CommitEdit
        {
            get
            {
                if (_commitEdit == null)
                {
                    _commitEdit = new RelayCommand(() => { this.EndEdit(); _inEdit = false; }, ()=> _inEdit);
                }
                return _commitEdit;
            }
        }

        public ICommand RollbackEdit
        {
            get
            {
                if (_rollbackEdit == null)
                {
                    _rollbackEdit = new RelayCommand(() => { this.CancelEdit(); _inEdit = false; }, () => _inEdit);
                }
                return _rollbackEdit;
            }
        }

        #region IDataErrorInfo Members

        public string Error
        {
            get { return string.Empty; }
        }

        /// <summary>
        /// Validate the property.
        /// </summary>
        /// <param name="columnName"></param>
        /// <returns></returns>
        public string this[string columnName]
        {
            get { return Validation<TestViewModel>.Validate(this, columnName); }
        }

        #endregion
    }
}
